package org.xy.vte;

import org.xy.vte.bean.ColumnNode;
import org.xy.vte.bean.TableVO;
import org.xy.vte.sql.SqlConfig;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname DataSource
 * @Description 数据库链接相关以及获取表结构
 * @Date 2024/6/21 10:50
 * @Created by jinxiaoyao
 */
public class DataSource {
   /* private String url = "jdbc:mysql://172.18.61.9:3316/matrix?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false";
    private String username = "matrix";

    private String password = "M92A_Csv8jaY";
    private String driverClassName = "com.mysql.cj.jdbc.Driver";*/

    private Connection connection;
    private PreparedStatement preparedStatement;
    private BaseConfig baseConfig;

    public DataSource(BaseConfig baseConfig) {
        this.baseConfig = baseConfig;
        getConnection();
    }

    public void getConnection() {
        try {
            Class<?> classDriver = Class.forName(baseConfig.getDriverClassName());
            Driver driver = (Driver) classDriver.newInstance();
            DriverManager.registerDriver(driver);
            connection = DriverManager.getConnection(baseConfig.getUrl(), baseConfig.getUsername(), baseConfig.getPassword());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }


    /**
     * 获取列信息
     *
     * @param table
     * @return
     * @throws SQLException
     */
    public List<ColumnNode> getColumnExecute(String table) throws SQLException {
        ResultSet resultSet = null;
        String sql = SqlConfig.getTableColumnsSql();
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, table);
        resultSet = preparedStatement.executeQuery();
        // 获取元数据
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 获取列数
        int columnCount = metaData.getColumnCount();
        List<ColumnNode> columnNodeList = new ArrayList<>();
        // 获取每一行数据
        while (resultSet.next()) {
            // 列名称
            String columnName = resultSet.getString("COLUMN_NAME");
            // 列类型
            String columnType = resultSet.getString("COLUMN_TYPE");
            // 列注释
            String columnComment = resultSet.getString("COLUMN_COMMENT");
            // 列注释
            String columnKey = resultSet.getString("COLUMN_KEY");
            columnNodeList.add(new ColumnNode(columnName, columnType, columnComment, columnKey));
            /* 获取所有的遍历
             */
            /*for (int i = 1; i <= columnCount; i++) {
                String columnNameq = metaData.getColumnName(i);
                // 获取列值
                String columnValue = resultSet.getString(i);
                System.out.println(columnNameq + "=" + columnValue);
            }*/

        }
        return columnNodeList;

    }

    /**
     * 获取当前表信息
     *
     * @param table
     * @return
     */
    public TableVO getTableExecute(String table) throws SQLException {
        ResultSet resultSet = null;
        String sql = SqlConfig.getTableInfoSql();
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, table);
        resultSet = preparedStatement.executeQuery();
        // 获取元数据
        ResultSetMetaData metaData = resultSet.getMetaData();
        // 获取列数
        int columnCount = metaData.getColumnCount();
        List<ColumnNode> columnNodeList = new ArrayList<>();
        String tableComment = "";
        // 获取每一行数据
        while (resultSet.next()) {
            // 表注释
            tableComment = resultSet.getString("TABLE_COMMENT");
            // 列名称
           /* String columnName = resultSet.getString("COLUMN_NAME");
            // 列类型
            String columnType = resultSet.getString("COLUMN_TYPE");
            // 列注释
            String columnComment = resultSet.getString("COLUMN_COMMENT");
            // 列注释
            String columnKey = resultSet.getString("COLUMN_KEY");
            columnNodeList.add(new ColumnNode(columnName, columnType, columnComment, columnKey));*/
            /* 获取所有的遍历
             */
         /*   for (int i = 1; i <= columnCount; i++) {
                String columnNameq = metaData.getColumnName(i);
                // 获取列值
                String columnValue = resultSet.getString(i);
                System.out.println(columnNameq + "=" + columnValue);
            }*/

        }
        TableVO tableVO = new TableVO();
        tableVO.setTableComment(tableComment);
        // 获取列信息
        List<ColumnNode> columnNodes = getColumnExecute(table);
        tableVO.setColumns(columnNodes);
        tableVO.setTableName(table);
        connection.close();
        preparedStatement.close();
        return tableVO;
    }



}
